package p2p.view.components
{
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.media.Video;
	import flash.net.NetStream;
	
	import mx.containers.Canvas;
	import mx.controls.Text;
	import mx.core.UIComponent;
	
	[Style(name="backgrColor", type="uint", inherit="no", format="Color")]
	[Style(name="bColor", type="uint", inherit="no", format="Color")]
	[Style(name="bThickness", type="Number", inherit="no")]
	
	[Event(name="streamChange", type="flash.events.Event")]
	
	public class VideoPlayer extends UIComponent
	{
		
		public static const STREAM_CHANGE:String = "streamChange";
		
		public function VideoPlayer()
		{
			super();
		}
		
		[Bindable]
		public function get showTip() : Boolean
		{
			return _showTip;
		}
		
		public function set showTip(value:Boolean) : void
		{
			_showTip = value;
			_showTipChanged = true;
		}
		
		[Bindable]
		public function get stream():NetStream
		{
			return _stream;
		}

		public function set stream(value:NetStream):void
		{
			_stream = value;
			_streamChanged = true;
			dispatchEvent(new Event(STREAM_CHANGE));
			invalidateProperties();
		}

		override protected function createChildren() : void
		{
			super.createChildren();
			if (!_tipHolder)
			{
				_tipHolder = new Canvas();
				_tipHolder.setStyle("backgroundColor", backgroundColor); 
				addChild(_tipHolder);
			}
			if (!_tip)
			{
				_tip = new Text();
				_tip.text = "CLICK ON USERS MONITORS FOR LARGE PREVIEWING";
				_tip.visible = _showTip;
				_tip.percentWidth = 100;
				_tip.setStyle("color", 0xffffff);
				_tip.setStyle("verticalCenter", 0);
				_tip.setStyle("horizontalCenter", 0);
				_tip.setStyle("textAlign", "center");
				_tip.setStyle("fontSize", 14);
				_tip.setStyle("fontWeight", "bold");
				_tipHolder.addChild(_tip);
			}
			if ( !_video )
			{
				_video = new Video();
				addChild( _video );
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
		{
			_video.width = unscaledWidth;
			_video.height = unscaledHeight;
			_tipHolder.width = unscaledWidth;
			_tipHolder.height = unscaledHeight;
			drawBorder(unscaledWidth, unscaledHeight);
		}
		
		override protected function commitProperties() : void
		{
			super.commitProperties();
			if (_streamChanged)
			{
				_streamChanged = false;
				_video.clear();
				_video.attachNetStream(_stream);
			}
			if (_showTipChanged)
			{
				_showTipChanged = false;
				_tip.visible = _showTip;
			}
		}
		
		protected function drawBorder(w:Number, h:Number) : void
		{
			var g:Graphics = graphics;
			g.clear();
			if (borderThickness > 0)
			{
				g.lineStyle(borderThickness, borderColor);
				g.drawRect(0, 0, w, h);
			}
		}
		
		private function get borderThickness() : Number
		{
			var result:Number = getStyle("bThickness");
			
			if (isNaN(result))
				result = 0;
			
			return result;
		}
		
		private function get backgroundColor() : uint
		{
			var result:uint = getStyle("backgrColor");
			return  result;
		}
		
		private function get borderColor() : uint
		{
			var result:uint = getStyle("bColor");
			return  result;
		}
		
		private var _video:Video;
		private var _tipHolder:Canvas;
		private var _tip:Text;
		private var _showTip:Boolean;
		private var _showTipChanged:Boolean;
		
		private var _stream:NetStream;
		private var _streamChanged:Boolean;
	}
}